<?php

class tx_zlib_model
{

	/**
	 * @var array
	 */
	protected $_data;

	/**
	 * @var tx_zlib_gateway
	 */
	protected $_gateway;

	/**
	 * @var boolean
	 */
	protected $_strictMode = false;

    /**
     * Constructor
     *
     * @param array|object $data
     */
	public function __construct($data = null)
	{
		if(!is_null($data)) {
            $this->populate($data);
		}
		$this->init();
	}

    /**
     * Initialize object
     *
     * Called from {@link __construct()} as final step of object instantiation.
     *
     * @return void
     */
    public function init()
    {
    }

	/**
	 * @param array|object $data
	 */
	public function populate($data)
	{
		if(is_object($data)) {
			$data = (array)$data;
		}
		if(!is_array($data)) {
			$exception = sprintf('Initial data in "%s"::populate() must be an array or object', get_class($this));
        	throw new Exception($exception);
		}
		foreach($data AS $key=>$value) {
			$this->_data[$key] = $value;
		}
	}

    /**
     * @param tx_zlib_gateway $gateway
     */
    public function setGateway(tx_zlib_gateway $gateway)
    {
    	$this->_gateway = $gateway;
    }

    /**
     * @return tx_zlib_gateway
     */
    public function getGateway()
    {
    	return $this->_gateway;
    }

    /**
     * @param boolean $flag
     */
    public function setStrictMode($flag)
    {
        $this->_strictMode = (bool)$flag;
    }

    /**
     * @return boolean
     */
    public function getStrictMode()
    {
        return $this->_strictMode;
    }

	/**
	 * @param string $name
	 * @param mixed $value
	 */
    protected function set($name, $value)
    {
		$this->_data[$name] = $value;
    }

	/**
	 * @param string $name
	 * @return mixed
	 */
    protected function get($name)
    {
		if(!array_key_exists($name, $this->_data)) {
			$message = sprintf('Property "%s" does not exist in class "%s"', $name, get_class($this));
            trigger_error($message, E_USER_NOTICE);
		}
		return $this->_data[$name];
    }

	/**
	 * @param string $name
	 * @param mixed $value
	 */
	protected function __set($name, $value)
	{
	    if($this->_strictMode) {
            $exception = sprintf('Strict mode is on in class "%s"', get_class($this));
            throw new Exception($exception);
	    }
		$this->_data[$name] = $value;
	}

	/**
	 * @param string $name
	 * @return mixed
	 */
	protected function __get($name)
	{
	    if($this->_strictMode) {
            $exception = sprintf('Strict mode is on in class "%s"', get_class($this));
            throw new Exception($exception);
	    }
		if(!array_key_exists($name, $this->_data)) {
			$message = sprintf('Property "%s" does not exist in class "%s"', $name, get_class($this));
            trigger_error($message, E_USER_NOTICE);
		}
		return $this->_data[$name];
	}

	/**
	 * @param string $name
	 * @return boolean
	 */
	protected function __isset($name)
	{
	    if($this->_strictMode) {
            $exception = sprintf('Strict mode is on in class "%s"', get_class($this));
            throw new Exception($exception);
	    }
		return isset($this->_data[$name]);
	}

	/**
	 * @param string $name
	 */
	protected function __unset($name)
	{
	    if($this->_strictMode) {
            $exception = sprintf('Strict mode is on in class "%s"', get_class($this));
            throw new Exception($exception);
	    }
		unset($this->_data[$name]);
	}

	/**
	 * @return array
	 */
	public function toArray()
	{
		return $this->_data;
	}

	/**
	 * @todo
	 * @return string
	 */
	public function toJson()
	{
	    return json_encode($this->_data);
	}

	/**
	 * @todo
	 * @return string
	 */
	public function toXml()
	{
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_model.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_model.php']);
}
